package com.globallogic.jira.generators;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.ofbiz.core.entity.GenericValue;

import com.atlassian.configurable.ValuesGenerator;
import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.project.Project;
import com.atlassian.jira.project.ProjectManager;
import com.atlassian.jira.project.version.Version;

/**
 * 
 * @author anton.pereverziev
 * @author yegor.chumakov
 */
public class FixVersionsGenerator implements ValuesGenerator {
    public Map<String, String> getValues(
	    @SuppressWarnings("rawtypes") Map userParams) {
	Map<String, String> versionsMap = new HashMap<String, String>();
	System.out.println("Project in version generator: "
		+ userParams.get("project").getClass());
	GenericValue genericProject = (GenericValue) userParams.get("project");
	ProjectManager projectManager = ComponentManager.getInstance()
		.getProjectManager();
	Project project = projectManager
		.getProjectObjByName((String) genericProject.get("name"));
	Collection<Version> versions = project.getVersions();
	if (versions != null && versions.size() != 0) {
	    for (Version version : versions) {
		versionsMap.put(version.getName().replace(" ", "+"),
			version.getName());
	    }
	}
	versionsMap.put("All", "All");
	return versionsMap;
    }
}